#ifndef __WEBPAGEQUERY_HPP__ 
#define __WEBPAGEQUERY_HPP__

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <utility>
#include <unordered_map>
#include <set>
#include "WebPage.hpp"

class WebPageQuery {
public:
    WebPageQuery(); // Constructor
    std::vector<std::string> doQuery(const std::string& str); // 执行查询，返回结果

private:
    // WordSegmentation _jieba; // jieba分词库对象
    static WebPageQuery *_query;
    // cppjieba::Jieba _jieba;
    std::unordered_map<int, WebPage> _pageLib; // 网页库
    std::unordered_map<int, std::pair<int, int>> _offsetLib; // 偏移库
    std::unordered_map<std::string, std::set<std::pair<int, double>>> _invertIndexTable; // 倒排索引表
    
    void loadLibrary(); // 加载库文件
    std::vector<double> getQuerywordsWeightVector(std::vector<std::string>& queryWords); // 计算查询词的权重值
    bool executeQuery(const std::vector<std::string>& queryWords, std::unordered_map<int, std::vector<double>>& resultVec); // 执行查询
    std::vector<std::string> createJson(std::vector<int>& docIdVec, const std::vector<std::string>& queryWords);
    std::string returnNoAnswer();
    static bool Compair(const std::pair<int, double> & lhs, const std::pair<int, double> & rhs) {
        return lhs.second > rhs.second;
    }
};

#endif